package fr.nantes.univ.DAO;

import java.util.List;

import javax.persistence.EntityManager;

import fr.nantes.univ.EMF;
import fr.nantes.univ.EntityBeans.User;

public class UserDAO {
	
	private static EntityManager instance = EMF.get().createEntityManager();
	
	public static User findById(String id) {
		User user = instance
				.createQuery("select u from User u where u.id = :id", User.class)
				.setParameter("id", id)
				.getSingleResult();
		return user;
	}
	
	public static List<User> findAll() {
		List<User> users = instance
				.createQuery("select u from User u", User.class)
				.getResultList();
		return users;
	}
	
	public static void insert(User user) {
		instance.getTransaction().begin();
		instance.persist(user);
		instance.getTransaction().commit();
	}

}
